
/*Object function to call */

var Utils =
{
	Browsers:
	{

		isIE: function()
		{
			if( this._isIE != null )
			{
				return this._isIE;
			}
			this._isIE = ( navigator.appName == "Microsoft Internet Explorer" );
			return this._isIE;
		},

		isOpera: function()
		{
			if( this._isOpera != null )
			{
				return this._isOpera;
			}
			this._isOpera = ( navigator.appName == "Opera" );
			return this._isOpera;
		},
		
		isIE6: function()
		{		  
		  if( this._ieIE6 == null )
		  {
		    var appVersion = navigator.appVersion;              		  
		    var browser = "MSIE";

			if(appVersion.indexOf(browser) != -1)
			{
			  var i = appVersion.indexOf(browser) + browser.length;
			    
			  while(isNaN(parseInt(appVersion.charAt(i)))) i++;
			    
			  for(var sd = ""; !isNaN(parseInt(appVersion.charAt(i))); i++)
			  {
			      sd += appVersion.charAt(i);
			  }
			      
			  var d = parseInt(sd);
			    
			  if(d == 6) 
			  {
			  	this._isIE6 = true;
			  }
			  else
			  {
			  	this._isIE6 = false;
			  }
			}
			else
			{
			  this._isIE6 = false;
			}
		  }
		  
		  return this._isIE6;
		},

		IE:
		{
			selectText: function( docObj, first, last )
			{
				var range = docObj.createTextRange();
				range.collapse( true );
				range.moveStart( 'character', first );
				range.moveEnd( 'character', last );
				range.select();
		    }
		}
	},

	KeyCodes:
	{
		ENTER: 13,
		TAB: 9,
		CTRL: 17,
		PAGE_UP: 33,
		PAGE_DOWN: 34	
	},

	isInArray: function( val, arr )
	{
		return (jQuery.inArray( val, arr ) != -1);
	},

	clearArray: function( arr )
	{
		arr.splice( 0, arr.length );
	},

	findIndexInArray: function( arr, val )
	{
		for( var i in arr )
		{
			if( arr[ i ] == val )
			{
				return i;
			}
		}
		return -1;
	},  	

	removeFromArray: function( arr, val )
	{
		var index = findIndexInArray( arr, val );

		if( index != -1 )
		{
			arr.splice( index, 1 );
		}	
	},  	

	request: function( params )
	{
		var cache = false;

		var context = params[ 'context' ];

		var successCaller = new ( this.Caller( context, params[ 'success' ] ) );
		var errorCaller = new ( this.Caller( context,  params[ 'error' ] ) );

		if( params[ 'cache' ] != null )
		{
			cache = params[ 'cache' ];
		}

		jQuery.ajax(	{	
							'url': params[ 'url' ],
							'success': successCaller.call,
							'error': errorCaller.call,
							'dataType': "json",
							'cache': cache 
						}	);
	},

	Functions:
	{
		initNull: function( func )
		{
			if( func != null )
			{
				return func;
			}
			
			return ( function(){} );
		}
	},

	Caller: function( context, func )
	{
		var _context = context;
		var _func = func;

		this.call = function( paramParams )
		{
			_func( _context, paramParams );
		}
	},
	Strings:
	{
		// works as normblocks filter
		// but without escaping
		normblocks: function( s )
		{
			var len = s.length;
			var result = "";
			var newlineFound = false;

			for( var i = 0; i < len; i++ )
			{
				var ch = s.charAt( i );

				if( ch == '\n' || ch == '\r' )
				{
					if( !newlineFound )
					{
						result += "<br><br>"
						newlineFound = true;
					}
				}
				else
				{
					result += ch;
					newlineFound = false;
				}
			}
			
			return result;	
		},

		getNumChars: function( s, ch, endPosition )
		{
			var endPos = endPosition;
			if( endPos == null )
			{
				endPos = s.length;
			}	

			s = s.substring( 0, endPos );

			var numCh = 0;
			j = 0;
			for( var i = s.search( ch ); i = s.search( ch ); true )
			{
				if( i == -1 ) break;			
				s = s.substring( i + 1, s.length );
				numCh++;

				j++;
				if( j > 20 ) break;	
			}

			return numCh;
		},

		getStr: function( s, numStr )
		{
			var arr = s.split( '\n' );
			return arr[ numStr ];		
		},

		getCol: function( s, pos )
		{
			var i = pos;
			var col = 0;
			while( i > 0 && s.charAt( i - 1 ) != '\n' )
			{
				i--;
				col++;
			}
			
			return col;
		},

		getRow: function( s, pos )
		{
			var i = pos;
			
			var row = 0
			while( i > 0 )
			{
				i--;
				if( s.charAt( i ) == '\n' ) row++;
			}

			return row;
		},

		getLastRow: function( s )
		{
			return this.getRow( s, s.length );
		},

		getRowLen: function( s, row )
		{
			var arr = s.split( '\n' );
			return arr[ row ].length;
		},

		rowColToPos: function( s, row, col )
		{
			var len = s.length;
			var iRow = 0;
			var iCol = 0;
			for( var i = 0; i < len; i++ )
			{
				if( iRow == row && iCol == col )
				{
					break;
				}

				var ch = s.charAt( i );
				if( ch == '\n' )
				{
					iRow++;
					iCol = 0;
				}
				else
				{
					iCol++;
				}
			}

			return i;
		}
	}
}